package com.zheng.study.web.controller;

import com.zheng.study.base.bean.Result;
import com.zheng.study.base.service.common.ResultService;
import com.zheng.study.base.utils.mapper.JsonMapper;
import com.zheng.study.company.service.*;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/20 。
 * ============================================================================
 */
public class BaseController implements Serializable {
	private static final long serialVersionUID = -1168716263633714797L;

	protected final static Logger logger = LoggerFactory.getLogger(BaseController.class);

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected ResultService resultService;

	@Autowired
	protected HttpServletResponse response;


	@Autowired
	protected AdminService adminService;

	@Autowired
	protected UserService userService;

	@Autowired
	protected DutyService dutyService;

	@Autowired
	protected PowerService powerService;

	@Autowired
	protected DepartmentService departmentService;

	@Autowired
	protected PostService postService;


	protected HttpHeaders defaultHeaders(MediaType mediaType) {
		HttpHeaders headers = new HttpHeaders();
		headers.setCacheControl("no-store");
		headers.setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), Charset.forName("utf-8")));
		headers.setDate(System.currentTimeMillis());
		headers.setPragma("no-cache");
		return headers;
	}

	protected HttpEntity ajaxJson(Map map){
		return new ResponseEntity<String>(JsonMapper.nonEmptyMapper().toJson(map), defaultHeaders(MediaType.APPLICATION_JSON), HttpStatus.OK);
	}

	protected HttpEntity ajax(Result result, MediaType mediaType) {
		return new ResponseEntity<String>(resultService.getBody(result), defaultHeaders(mediaType), result.getStatusCode());
	}

	protected HttpEntity ajaxJson(String message, HttpStatus statusCode) {
		return new ResponseEntity<String>(message, defaultHeaders(MediaType.APPLICATION_JSON), statusCode);
	}
	protected HttpEntity ajaxJson(Result result) {
		return new ResponseEntity<String>(resultService.getBody(result), defaultHeaders(MediaType.APPLICATION_JSON), result.getStatusCode());
	}

	protected HttpEntity ajaxJson(String str) {
		return new ResponseEntity<String>(str, defaultHeaders(MediaType.APPLICATION_JSON), HttpStatus.OK);
	}
	protected HttpEntity ajaxText(Result result) {
		return new ResponseEntity<String>(resultService.getBody(result), defaultHeaders(MediaType.TEXT_PLAIN), result.getStatusCode());
	}

	protected HttpEntity ajaxHtml(Result result) {
		return new ResponseEntity<String>(resultService.getBody(result), defaultHeaders(MediaType.TEXT_HTML), result.getStatusCode());
	}

	// AJAX输出文本，返回null
	public String ajaxText(String text) {
		return ajax(text, "text/plain");
	}

	// AJAX输出HTML，返回null
	public String ajaxHtml(String html) {
		return ajax(html, "text/html");
	}

	// AJAX输出XML，返回null
	public String ajaxXml(String xml) {
		return ajax(xml, "text/xml");
	}

	// 根据字符串输出JSON，返回null
	public String ajaxStringJson(String jsonString) {
		return ajax(jsonString, "application/json");
	}

	// 根据Map输出JSON，返回null
	public String ajaxStringJson(Map<String, String> jsonMap) {
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		return ajax(jsonObject.toString(), "application/json");
	}

	// AJAX输出，返回null
	public String ajax(String content, String type) {
		try {
			HttpServletResponse response = getResponse();
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 获取Response
	public HttpServletResponse getResponse() {
		return this.response;
	}

	public ResponseEntity<byte[]> fileDownload(InputStream in, String fileName) throws IOException {
		return fileDownload(in, fileName, MediaType.APPLICATION_OCTET_STREAM);
	}

	public ResponseEntity<byte[]> fileDownload(InputStream in, String fileName, MediaType mediaType) throws IOException {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(mediaType);
		headers.setContentDispositionFormData("attachment", new String(fileName.getBytes(), "ISO8859-1"));
		return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
	}

}
